﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Yipusoft.Duty.BLL;

public partial class BasicBase_Duty_DutyInput : System.Web.UI.Page
{
    private bool bl = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDate(); 
        }
            Bind();
        

    }

    private void BindDate()
    {
        DptMoth.Items.Clear();
        for (int n = DateTime.Now.Year; n <= DateTime.Now.Year + 1; n++)
        {
            DptYear.Items.Add(new ListItem(n.ToString() + "年", n.ToString()));
        }
        int Start = 1;
        HDay.Value = "1";
        this.DptYear.SelectedValue = DateTime.Now.Year.ToString();
        if (Request["year"] != null)
        {
            if (Request["year"].ToString().Trim().Equals(DateTime.Now.Year.ToString()))
            {
                Start = DateTime.Now.Month;
                HDay.Value = DateTime.Now.Day.ToString();
            }
            if (int.Parse(Request["year"].ToString()) > DateTime.Now.Year)
            {
                Start = 1;
                HDay.Value = "1";
            }
            this.DptYear.SelectedValue = Request["year"].ToString().Trim();
        }
        if (Request["year"] == null && Request["moth"] == null)
        {
            Start = DateTime.Now.Month;
            HDay.Value = DateTime.Now.Day.ToString();
        }     
        for (int i = Start; i <= 12; i++)
        {
            this.DptMoth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
            this.DptMoth.SelectedValue = "1";
        }
        if (Request["moth"] != null )
        {
            if (int.Parse(Request["year"].ToString()) >= DateTime.Now.Year)
            {
                if (int.Parse(Request["moth"].ToString()) > DateTime.Now.Month)
                {
                    Start = 1;
                    HDay.Value = "1";
                }
            }
            DptMoth.SelectedValue = Request["moth"].ToString();
        }
       
       
    }
    private void Bind()
    {
        int Year = int.Parse(DptYear.SelectedValue.Trim());
        int Moth = int.Parse(DptMoth.SelectedValue.Trim());
        //判断当前用所在组
        string Dept = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Dept == null || Dept.Length < 5)
            return;

        string strSql1 = "";
        if (Dept.Substring(4, 1).Equals("0"))
            strSql1 += "  Left(DepartmentID,5)='" + Dept.Substring(0, 5) + "'";
        else
            strSql1 += "  DepartmentID='" + Dept + "'";
        DataSet dst2 = Yipusoft.Duty.BLL.DutyType.GetList(strSql1);
        TableRow trw;
        TableCell cel1;
        TableCell cel2;
        string DeptID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (DeptID == null || DeptID.Length < 5)
            return;

        string strSql = string.Empty;
        if (DeptID.Substring(4, 1).Equals("0"))
            strSql = " Left(DepartmentID,5)='" + DeptID.Substring(0, 5) + "'";
        else
            strSql = " DepartmentID='" + DeptID + "'";


        DataSet dst = Yipusoft.Duty.BLL.DutyType.GetList(strSql);
        if (dst == null)
            return;

        tabList.BorderStyle = BorderStyle.Solid;
        tabList.BorderWidth = 1;
        //int Init = DateTime.Now.Day;
        int LastDay = DateTime.DaysInMonth(int.Parse(this.DptYear.SelectedValue), int.Parse(this.DptMoth.SelectedValue));
        int Start = int.Parse(HDay.Value);


        for (int i = Start; i <= LastDay; i++)
        {
            trw = new TableRow();


            //添加日期
            TableCell cel = new TableCell();
            cel.CssClass = "tabstyle";
            DateTime dt = new DateTime(Year, Moth, i);
            cel.Width = 20;
            //cel.Text =Helper.GetWeek(dt.DayOfWeek.ToString());
            cel.Controls.Add(new LiteralControl("<font color=red>" + Helper.GetWeek(dt.DayOfWeek.ToString()) + "</font>"));
            trw.Cells.Add(cel);

            //添加星期
            cel1 = new TableCell();
            cel1.CssClass = "tabstyle";
            cel1.Text = "" + i.ToString() + "日";
            cel1.Width = 30;
            cel1.Controls.Add(new LiteralControl("" + i.ToString() + "日"));
            trw.Cells.Add(cel1);


            cel2 = new TableCell();
            cel2.CssClass = "tabstyle";
            cel2.Width = 20;
            CheckBox chk = new CheckBox();
            if ((!Helper.GetWeek(dt.DayOfWeek.ToString()).Trim().Equals("六")) && (!Helper.GetWeek(dt.DayOfWeek.ToString()).Trim().Equals("日")))
                chk.Checked = true;

            cel2.Controls.Add(chk);
            trw.Cells.Add(cel2);


            AddCell(trw, i, dst2);
            tabList.Rows.Add(trw);
           //this.HRowsCount.Value = tabList.Rows.Count.ToString();


        }


        trw = new TableRow();
        for(int i=0;i<dst.Tables[0].Rows.Count;i++)
        {
            TableCell cel = new TableCell();
            cel.CssClass = "tabstyle";
            trw.Cells.Add(cel);
            cel.Controls.Add(new LiteralControl( "<font color=black>"+dst.Tables[0].Rows[i]["DTName"].ToString()+"</font>"));            
            
        }
        TableCell cel4 = new TableCell();
        cel4.CssClass = "tabstyle";
        trw.Cells.AddAt(0, cel4);
        TableCell cel5 = new TableCell();
        cel5.CssClass = "tabstyle";
        trw.Cells.AddAt(0, cel5);
        TableCell cel6 = new TableCell();
        cel6.CssClass = "tabstyle";
        trw.Cells.AddAt(0, cel6);
        this.tabList.Rows.AddAt(0, trw);

    }
    #region 添加值班岗位单元格
    //添加值班人员列表
    private void AddCell(TableRow trw, int day,DataSet dst)
    {
       int Year=int.Parse(DptYear.SelectedValue.Trim());
       int Moth=int.Parse(DptMoth.SelectedValue.Trim());

       int mothDay = DateTime.DaysInMonth(Year, Moth);
       DateTime Ldt = new DateTime(Year, Moth, mothDay);
       DateTime Start = new DateTime(Year, Moth, int.Parse(HDay.Value));

       DataSet dst1 = Yipusoft.Duty.BLL.DutyOrder.GetList(" DutyDate>= '" + Start + "' And  DutyDate<='" + Ldt + "' ");

        string str = string.Empty;
        
        
        for (int i = 0; i < dst.Tables[0].Rows.Count; i++)
        {
            TableCell cel = new TableCell();
            trw.Cells.Add(cel);
            cel.CssClass = "tabstyle";
            for (int n = 0; n < int.Parse(dst.Tables[0].Rows[i]["num"].ToString()); n++)
            {
               
                //cel.Controls.Add(dtp);
                //值班类别ID
                cel.Attributes["DutyType"] = dst.Tables[0].Rows[i]["DutyType"].ToString();

                //数量
                cel.Attributes["Num"] = dst.Tables[0].Rows[i]["num"].ToString();
                DateTime dt = new DateTime(Year, Moth, day);
                DataRow[] drw = dst1.Tables[0].Select(" DutyDate='" + dt + "' And DutyType='" + cel.Attributes["DutyType"].ToString() + "'");

                BindControl( dst.Tables[0].Rows[i]["DutyType"].ToString(), dst1, day,int.Parse(dst.Tables[0].Rows[i]["num"].ToString()),cel,drw);
                if (bl)
                    break;
            }

            
        }


    }
    #endregion

    #region 绑定下列列表
    private void BindControl( string paramid, DataSet dst, int day,int Count,TableCell cel,DataRow []drw)
    {
       
        int index = 0;
        if (dst != null)
        {
            index = drw.Length-1;
        }

        while (Count-1 >= 0)
        {
            DropDownList Dpt = new DropDownList();
            //DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, day);
            List<Yipusoft.Duty.Model.MemberStationInfo> list = Yipusoft.Duty.BLL.MemberStation.GetList(" a.DutyType='" + paramid + "'");
            Dpt.DataSource = list;
            Dpt.DataTextField = "UserName";
            Dpt.DataValueField = "UserID";
            Dpt.DataBind();
            Dpt.Items.Insert(0, new ListItem("----", "00"));
            
            if (dst != null)
            {
                             
               
                if (drw.Length > 0&&index>=0)
                {
                    
                        Dpt.SelectedValue = drw[index]["UserID"].ToString();
                        Dpt.ID = drw[index]["ID"].ToString();
                      
                    
                    
                }
                cel.Controls.Add(Dpt);

            }
            
            Count--;
        
                index--;
        }
        bl = true;

    }
    #endregion 
    #region 保存录入
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int Year = int.Parse(DptYear.SelectedValue.Trim());
        int Moth = int.Parse(DptMoth.SelectedValue.Trim());

        lblpromt.Text = "";
        //int day1 = DateTime.DaysInMonth(Year, Moth);
        //DateTime Last = new DateTime(Year, Moth, day1);
        //DateTime Start = new DateTime(Year, Moth, DateTime.Now.Day - 1);
        //int Falg = Yipusoft.Duty.BLL.DutyOrder.GetCountByWhere(" DutyDate BetWeen '"+Start+"' And '"+Last+"'");
        try
        {
            string UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;

            for (int i = 1; i < tabList.Rows.Count; i++)
            {
                int len = 0;
                for (int n = 3; n < tabList.Rows[i].Cells.Count; n++)
                {
                    for (int m = 0; m < int.Parse(tabList.Rows[i].Cells[n].Attributes["Num"].ToString()); m++)
                    {
                        if (!tabList.Rows[i].Cells[n].HasControls())
                            break;

                        DropDownList Dpt = tabList.Rows[i].Cells[n].Controls[m] as DropDownList;

                        if (Dpt == null)
                            break;
                        CheckBox box = tabList.Rows[i].Cells[2].Controls[0] as CheckBox;

                        if (box == null) break;
                        if (box.Checked)
                        {


                            Yipusoft.Duty.Model.DutyOrderInfo DutyOrder = new Yipusoft.Duty.Model.DutyOrderInfo();
                            DutyOrder.IsDelete = false;
                            DutyOrder.ID = Dpt.ID != null ? int.Parse(Dpt.ID) : -1;
                            DutyOrder.Operator = i == 1 ? "[" + User.Identity.Name + "]" + UserName : UserName;
                            DutyOrder.UpdateTime = DateTime.Now;
                            DutyOrder.UserName = Dpt.SelectedItem.Text.Trim();
                            DutyOrder.UserID = Dpt.SelectedValue.Trim();
                            DutyOrder.DutyType = tabList.Rows[i].Cells[n].Attributes["DutyType"].ToString();
                            LiteralControl lc = tabList.Rows[i].Cells[1].Controls[0] as LiteralControl;
                            if (lc == null || lc.Text.Trim() == "")
                                return;
                            int day = int.Parse(lc.Text.Replace("日", "").Trim());
                            DutyOrder.DutyDate = new DateTime(Year, Moth, day);
                            DutyOrder.Dutyorder = "0";
                            DutyOrder.Onduty_ld = len == 0 ? "1" : "0";

                            int flag = Yipusoft.Duty.BLL.DutyOrder.Add2(DutyOrder);
                            if (flag > 0)
                                lblpromt.Text = "保存成功";
                        }
                    }
                    len++;
                }
            }
        }
        catch (Exception ex) { lblpromt.Text = ex.Message; }
    }
    #endregion
    protected void DptYear_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        Response.Redirect("DutyInput.aspx?year=" + this.DptYear.SelectedValue);

    }
    protected void DptMoth_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        Response.Redirect("DutyInput.aspx?year=" + this.DptYear.SelectedValue + "&moth=" + this.DptMoth.SelectedValue);
    }
}
